/*
* @Author: sam
* @Date:   2017-10-18 13:24:37
* @Last Modified by:   sam
* @Last Modified time: 2017-10-24 19:31:32
* @Email: 83742368@qq.com
* @GitHub : https://github.com/AllanBian
*/
(function(win){
	var gaodeMap,tipBox,typeBox;
	var colors = Utils.color();
	var zoom = {
		'shi' : 8,
		'xian' : 9,
		'zhen' : 12,
		'cun' : 15
	};
	gaodeMap = new Class.Map("gaodeMap");
	tipBox = new Class.TipBox("tipBox");//内容介绍容器
	typeBox = new Class.TypeBox("typeBox");



	// //绘制市
	// var data1 = xiangtan;
	// var polygonObj1 = {
	// 	lng : 112.938653,
	// 	lat : 27.833165,
	// 	zoom : 8,
	// 	name : "湘潭市",
	// 	code : 123123,
	// 	data : data1,
	// 	fn : polygonClick
	// };

	// var data2 = shaoshanshi;
	// var polygonObj2 = {
	// 	lng : 112.521148,
	// 	lat : 27.918429,
	// 	zoom : 10,
	// 	name : "韶山市",
	// 	code : 13,
	// 	data : data2,
	// 	fn : polygonClick
	// };

	// var data3 = shaoshanxiang;
	// var polygonObj3 = {
	// 	lng : 112.486327,
	// 	lat : 27.901135,
	// 	zoom : 12,
	// 	name : "韶山县",
	// 	code : 1312,
	// 	data : data3,
	// 	fn : polygonClick
	// };

	// var data4 = shaoshancun;
	// var polygonObj4 = {
	// 	lng : 112.48065,
	// 	lat : 27.90337,
	// 	zoom : 13,
	// 	name : "韶山村",
	// 	code : 1331,
	// 	data : data4,
	// 	fn : polygonClick
	// };

	// var obj = {
	// 	'shi' : polygonObj1,
	// 	'xian' : polygonObj2,
	// 	'zhen' : polygonObj3,
	// 	'cun' :  polygonObj4
	// };

	// var content1 = {
	// 	'tipTitle' : '湘潭市',
	// 	'tipMsg' : '湘潭市描述信息',
	// 	'contact' : '',
	// 	'address' : '',
	// 	'detail' : './detail.html'
	// };
	// var content2 = {
	// 	'tipTitle' : '韶山县',
	// 	'tipMsg' : '韶山市描述信息',
	// 	'contact' : 'tel:13871224151',
	// 	'address' : './list.html',
	// 	'detail' : './detail.html'
	// };
	// var content3 = {
	// 	'tipTitle' : '韶山镇',
	// 	'tipMsg' : '韶山县描述信息',
	// 	'contact' : 'tel:13871234111',
	// 	'address' : './list.html',
	// 	'detail' : './detail.html'
	// };
	// var content4 = {
	// 	'tipTitle' : '韶山村',
	// 	'tipMsg' : '韶山村描述信息',
	// 	'contact' : 'tel:13871234551',
	// 	'address' : './515.html',
	// 	'detail' : './detail.html'
	// };

	// var content = {
	// 	'shi' : content1,
	// 	'xian' : content2,
	// 	'zhen' : content3,
	// 	'cun' :  content4
	// };

	//双击点击覆盖区域
	function enterPolygon(polygon){
		var tag;
		var countylist;
		var content = {},obj = {};
		tag = typeBox.next();
		countylist = JSON.parse(Utils.getLocalStorage(typeBox.status+gaodeMap.activitySendCode[typeBox.status]));
		if(tag && gaodeMap.canClick){
			if(typeBox.status === 'one'){
				gaodeMap.changeClickStatus(false);
				gaodeMap.hideOverlays();
				//绘制覆盖物
				obj = gaodeMap.stores[gaodeMap.prefix + polygon.code]['data'];
				gaodeMap.drawPolygon(obj);
				//保存当前显示的覆盖物code,便于实现隐藏所有当前显示的覆盖物
				gaodeMap.setOverlays(polygon.code);

				//装载tipBox数据
				content['tipTitle'] = obj['name']
				content['tipMsg'] = obj['name'] + '相关消息';
				content['contact'] = obj['tel'];
				content['address'] = obj['name'];
				content['detail'] = './detail.html?code=' + obj['code'];
				content['lng'] = obj['lng'];
				content['lat'] = obj['lat'];
				content['status'] = typeBox.status;
				content['code'] = obj['code'];

				tipBox.changeContent(content);
				gaodeMap.saveActivitySendCode(typeBox.status,obj['code']);

				window.setTimeout(function(){
					gaodeMap.changeClickStatus(true);
				},500);
			}else{
				if(countylist === null){
					drawAjax();
				}else{
					drawFun(countylist);
				}
			}
		}
	}

	//单击点击区域
	function polygonClick(map,polygon){
		// var code = map.activityCode;
		// var obj = {},content = {};
		// var store = map.stores[map.prefix + code]['data'];
		// obj['lng'] 			= store['lng'];
		// obj['lat'] 			= store['lat'];
		// obj['zoom'] 		= zoom[typeBox.status];
		// content['tipTitle'] = store['name'];
		// content['tipMsg'] 	= store['name'] + '相关消息';
		// content['contact'] 	= '13861737911';
		// content['address'] 	= store['name'];
		// content['detail'] 	= './detail.html';
		// content['lng'] 		= store['lng'];
		// content['lat'] 		= store['lat'];
		// content['status'] 	= typeBox.status;
		// content['fn'] 		= enterPolygon;

		// tipBox.changeContent(content);
		// gaodeMap.saveActivitySendCode(typeBox.status,store['code']);
		// console.log(gaodeMap);

		enterPolygon(polygon);
	}

	//点击地图的事件
	function mapClickFun(map){
		var tag;
		var countylist;
		tag = typeBox.prev();
		countylist = JSON.parse(Utils.getLocalStorage(typeBox.status+gaodeMap.activitySendCode[typeBox.status]));
		if(tag && gaodeMap.canClick){
			gaodeMap.setActivity(gaodeMap.activitySendCode[typeBox.status]);
			if(countylist === null){
				drawAjax();
			}else{
				drawFun(countylist);
			}
		}
	}
	var loading = document.querySelector('.loading');

	//ajax获取数据绘图
	function drawAjax(){
		gaodeMap.changeClickStatus(false);

		Utils.ajax({
				type : 'get',
				url : Configs.api.getCity,
				data : { code : gaodeMap.activityCode, types : typeBox.status},
				beforeSend : function(){
					loading.classList.add('show');
				},
				afterSend : function(){
					loading.classList.remove('show');
				},
				success : function(json){
					var data = JSON.parse(json);

					if(data.status === Configs.status.success){
						if(data.data.countylist.length > 0){
							Utils.setLocalStorage(typeBox.status+gaodeMap.activityCode,JSON.stringify(data.data.countylist));
							drawFun(data.data.countylist);
						}
					}

					if(data.status === Configs.status.empty){
						window.setTimeout(function(){
							gaodeMap.changeClickStatus(true);
						},500);
						console.log('暂无数据');
					}
					// console.log(data);
					console.log(gaodeMap);
					//console.log(typeBox);
				}
		});
	}

	//绘制方法
	function drawFun(countylist){
		var obj = {},content = {};
		var cunArr = [],xiangObj = {};

		if(countylist.length > 0){
			//隐藏所有覆盖物
			gaodeMap.hideOverlays();
			//画地图
			for(var i = 0; i < countylist.length; i++){
				//装载每个覆盖物的数据
				obj = {};
				obj['lng'] = countylist[i]['Longtude'];
				obj['lat'] = countylist[i]['Latitude'];
				obj['zoom'] = zoom[typeBox.status];
				obj['name'] = countylist[i]['Name'];
				obj['code'] = countylist[i]['Code'];
				obj['data'] = countylist[i]['Ploygnp'];
				obj['color'] = colors[i%colors.length];
				obj['tel'] = '13861737911';
				obj['fnclick'] = polygonClick;
				//装载tipBox数据
				content['tipTitle'] = countylist[i]['Name'];
				content['tipMsg'] = countylist[i]['Name'] + '相关消息';
				content['contact'] = '13861737911';
				content['address'] = countylist[i]['Name'];
				content['detail'] = './detail.html';
				content['lng'] = countylist[i]['Longtude'];
				content['lat'] = countylist[i]['Latitude'];
				content['status'] = typeBox.status;
				content['code'] = countylist[i]['Code'];

				//如果是市的话设置中心点
				if(typeBox.status === "shi"){
					if(i === 0){
						window.setTimeout(function(){
							if(obj['lng'] !== null){
								gaodeMap.setZoomAndCenter(obj);
							}else{
							}
							//保存最后一次的市,县,镇,村的数据
							gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
						},0);
					}
				}else if(typeBox.status === "xian"){
					gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
				}else if(typeBox.status === "zhen"){
					gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
				}else if(typeBox.status === "cun"){
					cunArr.push(countylist[i]['Name']);
				}else{
					// window.setTimeout(function(){
					// 	tipBox.changeContent(content);
					// 	gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
					// },0);
				}

				//绘制覆盖物
				gaodeMap.drawPolygon(obj);
				//保存当前显示的覆盖物code,便于实现隐藏所有当前显示的覆盖物
				gaodeMap.setOverlays(countylist[i]['Code']);
			}

			if(typeBox.status === "cun"){
				xiangObj = gaodeMap.stores[gaodeMap.prefix + gaodeMap.activityCode]['data'];
				//装载tipBox数据
				content['tipTitle'] = xiangObj['name'];
				content['tipMsg'] = "下属村有:" + cunArr.join();
				content['contact'] = xiangObj['tel'];
				content['address'] = xiangObj['name'];
				content['detail'] = './list.html?code=' + xiangObj['code'];
				content['lng'] = xiangObj['lng'];
				content['lat'] = xiangObj['lat'];
				content['status'] = typeBox.status;
				content['code'] = xiangObj['code'];
				window.setTimeout(function(){
					tipBox.changeContent(content);
					gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
				},0);
			}else{
				content['status'] = typeBox.status;
				tipBox.changeContent(content);
			}

		}
		window.setTimeout(function(){
			gaodeMap.changeClickStatus(true);
		},500);

	}


	// //初始化区域
	//gaodeMap.drawPolygon(polygonObj1);
	gaodeMap.mapClick(mapClickFun);
	drawAjax();

	Utils.getGeoLocation('geoData');


})(window);

//打电话
function makeCall(dom){
	Utils.makeCall(dom);
}

var listBox = new Class.ListBox('listBox');
//打开app
function openApp(dom){
	var openTypeBox = document.querySelector('.openTypeBox');
	var lng = dom.getAttribute('lng');
	var lat = dom.getAttribute('lat');
	var dest_name = dom.getAttribute('dest_name');
	var dest = {
		'lng' : lng,
		'lat' : lat,
		'dest_name' : dest_name
	};
	var ddDom;
	if(!openTypeBox.classList.contains('bok')){
		openTypeBox.classList.add('bok');
		window.setTimeout(function(){
			openTypeBox.classList.add('show');
			ddDom = document.querySelectorAll('.openType dd');
			for(var i = 0; i < ddDom.length; i++){
			 	(function(n){
			 		if(ddDom[n].onclick === null){
				 		ddDom[n].onclick = function(){
				 			listBox.changeMapType(this.getAttribute('type'));
				 			if(openTypeBox.classList.contains('bok')){
				 				openTypeBox.classList.remove('show');
				 				window.setTimeout(function(){
				 					openTypeBox.classList.remove('bok');
				 				},200)
				 			}
				 			listBox.openMapApp(dest);
				 			console.log(listBox);
				 		};
			 		}

			 	})(i);
			}
			openTypeBox.onclick = function(){
	 			if(openTypeBox.classList.contains('bok')){
	 				openTypeBox.classList.remove('show');
	 				window.setTimeout(function(){
	 					openTypeBox.classList.remove('bok');
	 				},200)
	 			}
			};
		},0);
	}

}